package com.lks.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.lks.common.controller.BaseController;
import com.lks.common.util.ConstantFinalUtil;
import com.lks.common.util.PageInfoUtil;
import com.lks.users.pojo.AAdmins;
import com.lks.users.service.IUsersService;

@Controller
@RequestMapping("/back/admins/")
public class AdminsBackController extends BaseController
{
	@Resource
	private IUsersService usersService;

	/**
	 * 用户首页
	 * 
	 * @return
	 */
	@RequestMapping("index.htm")
	public String index()
	{
		return "/back/index";
	}

	/**
	 * 左侧的菜单
	 */
	@RequestMapping("leftMenu.htm")
	public String leftMenu()
	{
		return "/back/leftMenu";
	}

	/**
	 * 打开用户插入页面
	 * 
	 * @return
	 */
	@RequestMapping("adminsInsert.htm")
	public String adminsInsert()
	{
		return "/back/adminsInsert";
	}

	/**
	 * 用户插入页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "adminsInsertSubmit.htm", produces = "text/html;charset=UTF-8")
	public @ResponseBody String adminsInsertSubmit(AAdmins admins, HttpServletRequest request)
	{
		/**
		 * 用户插入的页面，设置密码，设置时间
		 */
		admins.setPassword(this.encryptUtil.encrptString(admins.getPassword()));
		admins.setCreateTime(new Date());
		admins.setUpdateTime(new Date());
		admins.setLastTime(new Date());

		JSONObject resultJSON = this.usersService.insertOneAdminsService(admins);
		ConstantFinalUtil.LOGGER.info("添加用户信息：{}", resultJSON);

		resultJSON = this.returnJuiJSON(request, resultJSON.getString("info"));
		return resultJSON.toJSONString();

	}

	/**
	 * 管理员的列表
	 */
	@RequestMapping("adminsList.htm")
	public String adminsList(HttpServletRequest request)
	{
		// 接受参数
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");

		if (status != null)
		{
			status = "";
		}
		if (st == null)
		{
			st = "";
		}
		if (ed == null)
		{
			ed = "";
		}

		Date stDate = null;
		Date edDate = null;

		if (!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateFormatUtil.parseDateTime(st);
			edDate = this.dateFormatUtil.parseDateTime(ed);
		}
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 查询数据 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		List<AAdmins> adminsList = this.usersService.findCondListAdminsService(pageInfoUtil, condMap);

		/* 存储结果 */
		request.setAttribute("adminsList", adminsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		System.out.println("查询的用户数据" + adminsList);
		/* 存储条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("stDate", stDate);
		request.setAttribute("edDate", edDate);

		return "/back/adminsList";
	}

	/**
	 * 用户退出
	 */
	@RequestMapping("logout.htm")
	public String logout(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		/**
		 * 其中用户退出: 1.清除其中的登录时间和admins
		 */
		session.removeAttribute("admins");
		session.removeAttribute("lastTime");

		session.setAttribute("info", "用户退出成功");
		return "redirect:/adminsLogin.htm";
	}
}
